package com.vcar.LDiceClient.core
{	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.system.ApplicationDomain;
	import flash.text.*;
	
	import mx.controls.Button;
	import mx.core.BitmapAsset;
	import mx.core.UIComponent;
	import mx.effects.Fade;
	import mx.events.EffectEvent;
	import mx.events.ModuleEvent;
	import mx.managers.FocusManager;
	import mx.modules.*;
	public class ModWindow extends UIComponent
	{				            

		public var backImageCls1:Class;
		

		public var backImageCls2:Class;
		
		private var bgInner1:Rectangle=new Rectangle(23,15,478,310);
		private var bgInner2:Rectangle=new Rectangle(12,12,235,108);
		
		private var winType:int;
		
		public var moduleUrl:String;
		public var data:Object;
		public var info:IModuleInfo;
		public var moduleContent:ModContent;
		private var applicationDomain:ApplicationDomain;				
		private var background:ScaleImage;
		private var endEffect:Fade;
		
		private var initWidth:Number;
		private var initHeight:Number;
		private var bCenter:Boolean;
		//private var loadingClip:LoadingClip;
		private var loadingClip:TextField;
		
		private var cancelButton:Button;
		//第一个执行
		public function ModWindow(modUrl:String,data:Object,application:ApplicationDomain,t:int,w:Number,h:Number,center:Boolean):void
		{
			super();
			//DataManager.data.resetUserHandle(); //有窗口弹出，证明用户有操作，计时器重置；
			moduleUrl=modUrl;
			this.data=data;	
			bCenter=center;
			
			applicationDomain=application;
			
			initWidth=w;
			initHeight=h;
			
			width=(w==-1 ? 400 : w)	;
			height=(h==-1 ? 200 : h);	
			
			winType = t;
			
			this.addEventListener(Event.REMOVED_FROM_STAGE,unload,false,0,true);		
			
			this.setFocus();																			         
		}
		//第二个执行
		override protected function createChildren():void
		{
			var imgObj:BitmapAsset = (winType==1)?(new backImageCls1()):(new backImageCls2());
			var bmd:BitmapData = imgObj.bitmapData;						
			var inner:Rectangle = (winType==1)?(bgInner1):(bgInner2);
			background=new ScaleImage(bmd,inner);
			background.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown,false,0,true);
			background.addEventListener(MouseEvent.MOUSE_UP,onMouseUp,false,0,true);
			addChild(background);
			background.visible=false;	
			
			loadingClip=new TextField();
			loadingClip.height=30;
			loadingClip.autoSize=TextFieldAutoSize.CENTER;
			loadingClip.text="正在加载..0%";
			loadingClip.visible=false;
			addChild(loadingClip);
			
			cancelButton=new Button();
			cancelButton.label="取消";
			cancelButton.styleName="mailbta01";
			cancelButton.width=60;
			cancelButton.height=24;
			cancelButton.addEventListener(MouseEvent.CLICK,closeIt,false,0,true);
			cancelButton.visible=false;
			addChild(cancelButton);
			cancelButton.setFocus();
			
			super.createChildren();								
		}
		private function onModuleProgress(e:ModuleEvent):void{
			if(e.bytesLoaded==e.bytesTotal){
				loadingClip.text="加载完成";
				return;
			}
			loadingClip.text="正在加载.."+ Math.floor(e.bytesLoaded/e.bytesTotal*100).toString()+"%";
		}
		//第三个执行
		override protected function initializationComplete():void
		{			
			super.initializationComplete();			
			
			var iw:Number;
			var ih:Number;
			if(initWidth==-1) iw=400;
			else iw=initWidth;
			if(initHeight==-1) ih=200;
			else ih=initHeight;
			
			background.visible=true;
			background.mouseEnabled=false;
			background.setSize(iw,ih);
			loadModule();
			background.mouseEnabled=true;
			if(bCenter && moduleContent==null){
				x=int ((stage.stageWidth-iw)/2);
				y=int((stage.stageHeight-ih)/2);
			}  
			
		}
		
		/*
		Unload
		*/
		public function unload(e:Event=null):void
		{
			if(moduleContent!=null){								
				moduleContent.dataObject=null;				
				removeChild(moduleContent);
				moduleContent=null;	
			}
			
			if(endEffect!=null){
				endEffect.stop();
				endEffect=null;
			} 		
			
			info.removeEventListener(ModuleEvent.READY, onModuleReady);			
			info.unload();
			info=null;	
			
			background.unload();
			removeChild(background);						
		}
		
		/*
		Load Module
		*/
		private function loadModule():void
		{
			info = ModuleManager.getModule(moduleUrl);
			info.addEventListener(ModuleEvent.READY, onModuleReady,false,0,true);  
			info.addEventListener(ModuleEvent.PROGRESS, onModuleProgress,false,0,true); 
			info.addEventListener(ModuleEvent.ERROR, onModuleLoadError,false,0,true);	        	          
			info.load(applicationDomain);
			
			loadingClip.x=(background.width-loadingClip.width)/2;
			loadingClip.y=(background.height-loadingClip.height)/2;
			loadingClip.visible=true;
			
			cancelButton.x=(background.width-cancelButton.width)/2
			cancelButton.y=background.height-cancelButton.height-20;
			cancelButton.visible=true;
			
			
		}
		private function onModuleLoadError(e:ModuleEvent):void
		{
			var tf:TextField=new TextField();
			//	tf.width=300;
			tf.wordWrap=false;
			tf.multiline=true;
			tf.autoSize=TextFieldAutoSize.CENTER;			
			tf.text="载入出错，请关闭窗口重试!";
			tf.selectable=false;
			tf.y=30;
			if(width<=tf.width){
				tf.width=width-10;
			}
			tf.x=(width-tf.width)/2;
			
			addChild(tf);
			
			removeChild(loadingClip);							
		}
		
		private function onModuleReady(e:ModuleEvent):void
		{
			this.setFocus();
			moduleContent=info.factory.create() as ModContent;
			if(data!=null) moduleContent.dataObject=data;
			this.focusManager=new FocusManager(moduleContent);//修正 mod载入 focusManger 错误
			
			if(initWidth==-1) width=moduleContent.width;
			else width=initWidth;
			if(initHeight==-1) height=moduleContent.height;
			else height=initHeight;			
			try{
				removeChild(loadingClip);
				removeChild(cancelButton);
			} catch(e:Error){}
			
			//应策划需求，去掉展开的动画效果	
			background.setSize(width,height);
			background.mouseEnabled=true;	
			if(bCenter){
				x=int ((stage.stageWidth-width)/2);
				//y=int((stage.stageHeight-height)/2);
				y=80;
			} 
			onModInit();																	
		}
		private function onModInit():void
		{			
			addChild(moduleContent);
		}
		
		/*
		Drag
		*/
		private function onMouseDown(e:MouseEvent):void
		{
			PopUpManager.bringToFront(this);
			//startDrag();
		}
		
		private function onMouseUp(e:MouseEvent):void
		{
			//stopDrag();
		}
		
		/*
		Remove Effect
		*/
		public function remove(animation:Boolean=true):void
		{
			if(animation){
				endEffect=new Fade(this);
				endEffect.addEventListener(EffectEvent.EFFECT_END,onRemoveEffectEnd,false,0,true);
				endEffect.duration=50;
				endEffect.alphaTo=0;
				endEffect.play();
			}else{
				onRemoveEffectEnd();
			}
		}
		private function onRemoveEffectEnd(e:EffectEvent=null):void
		{
			PopUpManager.removePopUp(this);
		}
		
		private function closeIt(e:Event=null):void
		{
			ModWindowManager.getInstance().removePopUp(this);
		}
	}
}

